<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>无限极分类自关联递归 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="无限极分类自关联简介：无限极分类就像一个树状图，从顶级父类开始向下延伸，延伸的点可以看做一个子类，但同时也可以作为父类向下继续延伸    创建一个模型类  123456789class Cate(models.Model):    # 分类名称    name &#x3D; models.CharField(max_length&#x3D;500)    # 父级id    pid &#x3D;">
<meta property="og:type" content="article">
<meta property="og:title" content="无限极分类自关联递归">
<meta property="og:url" content="https://mourning-soul.gitee.io/hexo/2021/02/04/%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="无限极分类自关联简介：无限极分类就像一个树状图，从顶级父类开始向下延伸，延伸的点可以看做一个子类，但同时也可以作为父类向下继续延伸    创建一个模型类  123456789class Cate(models.Model):    # 分类名称    name &#x3D; models.CharField(max_length&#x3D;500)    # 父级id    pid &#x3D;">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-02-03T16:00:00.000Z">
<meta property="article:modified_time" content="2021-02-05T04:39:08.777Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="-blocker">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/hexo/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/hexo/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/hexo/css/style.css">

  
    
<link rel="stylesheet" href="/hexo/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo/">Home</a>
        
          <a class="main-nav-link" href="/hexo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://mourning-soul.gitee.io/hexo"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-无限极分类自关联递归" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/hexo/2021/02/04/%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/" class="article-date">
  <time class="dt-published" datetime="2021-02-03T16:00:00.000Z" itemprop="datePublished">2021-02-04</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      无限极分类自关联递归
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="无限极分类自关联"><a href="#无限极分类自关联" class="headerlink" title="无限极分类自关联"></a>无限极分类自关联</h2><h3 id="简介：无限极分类就像一个树状图，从顶级父类开始向下延伸，延伸的点可以看做一个子类，但同时也可以作为父类向下继续延伸"><a href="#简介：无限极分类就像一个树状图，从顶级父类开始向下延伸，延伸的点可以看做一个子类，但同时也可以作为父类向下继续延伸" class="headerlink" title="简介：无限极分类就像一个树状图，从顶级父类开始向下延伸，延伸的点可以看做一个子类，但同时也可以作为父类向下继续延伸"></a>简介：无限极分类就像一个树状图，从顶级父类开始向下延伸，延伸的点可以看做一个子类，但同时也可以作为父类向下继续延伸</h3><hr>
<br>

<ul>
<li>创建一个模型类</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">class Cate(models.Model):</span><br><span class="line">    # 分类名称</span><br><span class="line">    name &#x3D; models.CharField(max_length&#x3D;500)</span><br><span class="line">    # 父级id</span><br><span class="line">    pid &#x3D; models.IntegerField()</span><br><span class="line"></span><br><span class="line">    # 表名</span><br><span class="line">    class Meta:</span><br><span class="line">        db_table &#x3D; &#39;cate&#39;</span><br></pre></td></tr></table></figure>
<hr>
<br>

<ul>
<li>序列化器类</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">from rest_framework import serializers</span><br><span class="line">from .models import Cate</span><br><span class="line">class CateSer(serializers.ModelSerializer):</span><br><span class="line">    class Meta:</span><br><span class="line">        model &#x3D; Cate</span><br><span class="line">        fields &#x3D; &quot;__all__&quot;</span><br></pre></td></tr></table></figure>
<ul>
<li>django序列化出来的类不具备层级结构，提前写好用来递归的方法</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">def xTree(datas):</span><br><span class="line">    lists &#x3D; []</span><br><span class="line">    tree &#x3D; &#123;&#125;</span><br><span class="line">    parent_id &#x3D; &#39;&#39;</span><br><span class="line">    for i in datas:</span><br><span class="line">        item &#x3D; i</span><br><span class="line">        print(item)</span><br><span class="line">        tree[item[&#39;id&#39;]] &#x3D; item</span><br><span class="line">    root &#x3D; None</span><br><span class="line">    for i in datas:</span><br><span class="line">        obj &#x3D; i</span><br><span class="line">        if not obj[&#39;pid&#39;]:</span><br><span class="line">            root &#x3D; tree[obj[&#39;id&#39;]]</span><br><span class="line">            lists.append(root)</span><br><span class="line">        else:</span><br><span class="line">            parent_id &#x3D; obj[&#39;pid&#39;]</span><br><span class="line">            if &#39;childlist&#39; not in tree[parent_id]:</span><br><span class="line">                tree[parent_id][&#39;childlist&#39;] &#x3D; []</span><br><span class="line">            tree[parent_id][&#39;childlist&#39;].append(tree[obj[&#39;id&#39;]])</span><br><span class="line">    return lists</span><br></pre></td></tr></table></figure>
<ul>
<li>写一个视图接口，用来从数据库中读取数据</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">class MyTree(APIView):</span><br><span class="line">    def get(self, request):</span><br><span class="line">        # 获取订单列表</span><br><span class="line">        catelist &#x3D; Cate.objects.all()</span><br><span class="line">        # 序列化</span><br><span class="line">        catelist_ser &#x3D; CateSer(catelist, many&#x3D;True)</span><br><span class="line"></span><br><span class="line">        mylist &#x3D; xTree(catelist_ser.data)</span><br><span class="line">        print(mylist)</span><br><span class="line">        return Response(mylist)</span><br></pre></td></tr></table></figure>
<ul>
<li>postman测试结果</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">[</span><br><span class="line">    &#123;</span><br><span class="line">        &quot;id&quot;:1,</span><br><span class="line">        &quot;name&quot;:&quot;品牌&quot;,</span><br><span class="line">        &quot;pid&quot;:0,</span><br><span class="line">        &quot;childlist&quot;:[</span><br><span class="line">            &#123;</span><br><span class="line">                &quot;id&quot;:2</span><br><span class="line">                &quot;name&quot;:&quot;美邦&quot;</span><br><span class="line">                &quot;pid&quot;:1</span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://mourning-soul.gitee.io/hexo/2021/02/04/%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/" data-id="ckkt9opgc000mi4twh9tgf6sy" data-title="无限极分类自关联递归" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/hexo/2021/02/05/%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          视频直播
        
      </div>
    </a>
  
  
    <a href="/hexo/2021/02/03/webshell/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">webshell</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/hexo/tags/blocker/" style="font-size: 10px;">-blocker</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/02/">February 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/01/">January 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo/2021/02/05/%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/">视频直播</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/04/%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/">无限极分类自关联递归</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/03/webshell/">webshell</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/">redis键空间事件</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/">redis发布者订阅者</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/hexo/js/jquery-3.4.1.min.js"></script>



  
<script src="/hexo/fancybox/jquery.fancybox.min.js"></script>




<script src="/hexo/js/script.js"></script>





  </div>
</body>
</html>